package com.winning.sx.microframework.controller.sysmanage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.SelectModel;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.LoginSession;
import com.winning.sx.microframework.model.manage.UserManageModel;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.sysmanage.UserManageService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nicholasyan on 16/5/18.
 */
@Controller
@RequestMapping(value = "usermanage")
public class UserManageController {
    @Autowired
    private UserManageService userManageService;
    private ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping(value = {"/", ""})
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("manage/usermanage");
      /* List<SelectModel> deptList = userManageService.getDeptList();
        deptList.add(0, new SelectModel("选择部门", "-1"));
        mv.addObject("deptList", deptList);*/
        return mv;
    }

    @RequestMapping(value = {"/userInfo"})
    public ModelAndView userInfo() {
        ModelAndView mv = new ModelAndView("manage/user-info");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cUserCode", ((LoginSession) SecurityUtils.getSubject().getSession().getAttribute("loginSession")).getcUserCode());
        map.put("cUserName", ((LoginSession) SecurityUtils.getSubject().getSession().getAttribute("loginSession")).getcUserName());
        map.put("cDeptName", ((LoginSession) SecurityUtils.getSubject().getSession().getAttribute("loginSession")).getcDeptName());
        mv.addObject("sessionUser", map);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = {"/getSessionUser"})
    public AjaxDataResponseMessage getSessionUser() {
        AjaxDataResponseMessage ajaxResponseMessage = new AjaxDataResponseMessage();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cUserCode", ((LoginSession) SecurityUtils.getSubject().getSession().getAttribute("loginSession")).getcUserCode());
        map.put("cUserName", ((LoginSession) SecurityUtils.getSubject().getSession().getAttribute("loginSession")).getcUserName());
        map.put("cDeptName", ((LoginSession) SecurityUtils.getSubject().getSession().getAttribute("loginSession")).getcDeptName());
        ajaxResponseMessage.setData(map);
        ajaxResponseMessage.setSuccess(true);
        return ajaxResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryAll"})
    public AjaxTableResponseMessage query() {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        try {
            DataTable table = userManageService.queryUsers();

            ajaxTableResponseMessage.setDatas(table.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) table.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping("/{type}")
    public AjaxDataResponseMessage userOperation(@PathVariable("type") String type, UserManageModel userModel) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        try {
            userManageService.editUser(type, userModel);

            ajaxDataResponseMessage.setData(userModel);
            ajaxDataResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg(e.getCause().getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryURassociation"})
    public AjaxTableResponseMessage queryAsso(@RequestParam Map<String, Object> map) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        DataTable table = null;
        try {

            table = userManageService.queryURasso(map);

            ajaxTableResponseMessage.setDatas(table.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) table.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping("/save")
    public AjaxDataResponseMessage saveAsso(String datas) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, Object> map = null;
        int result = -1;
        try {
            if (datas != null) {
                map = this.objectMapper.readValue(datas, Map.class);
            }
            result = userManageService.saveAsso(map);
            ajaxDataResponseMessage.setData(result);
            if (result > -1)
                ajaxDataResponseMessage.setSuccess(true);
            else
                ajaxDataResponseMessage.setSuccess(false);
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("设置角色失败");
        }
        return ajaxDataResponseMessage;
    }

    //@ResponseBody
    //@RequestMapping("/syncDept")
    //public AjaxDataResponseMessage syncDept(String datas) throws CustomException {
    //    AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
    //    int result = -1;
    //    try {
    //        result = userManageService.syncDept();
    //        ajaxDataResponseMessage.setData(result);
    //        ajaxDataResponseMessage.setSuccess(true);
    //    } catch (Exception e) {
    //        ajaxDataResponseMessage.setSuccess(false);
    //        ajaxDataResponseMessage.setErrorMsg("同步部门字典失败");
    //    }
    //    return ajaxDataResponseMessage;
    //}
}
